//
//  WBStatusViewModel.swift
//  新浪微博练习
//
//  Created by 韩旭 on 16/11/28.
//  Copyright © 2016年 韩旭. All rights reserved.
//

import UIKit

class WBStatusViewModel: NSObject {
    var statusModel: WBStatusModel
    
    var vipImage: UIImage?
    
    var levelImage: UIImage?
    
    var sourceString: String?
    
    var timeString:String?
    
    var retweetedText: String?
    
    var pictureViewSize: CGSize = CGSize.zero
    
    var pic_urls: [WBPictureModel]?

    init (statusModel: WBStatusModel) {
        self.statusModel = statusModel
        super.init()
        //处理用户信息
        dealWithUser()
        //处理原创微博信息
        dealWithStauts()
        //label文字
        dealWithRetweeted()
        //计算配图的size
        caculatePictureSize()
    }
    //计算配图的size
    func caculatePictureSize (){
        //如果原创微博有图片.pic_urls就保存原创图片
        if let pictures = statusModel.pic_urls, pictures.count > 0 {
            pic_urls = pictures
        }
        //如果转发微博有图片,pic_urls就保存转发图片
        if let pictures = statusModel.retweeted_status?.pic_urls, pictures.count > 0  {
            pic_urls = pictures
        }
        if let pictures = pic_urls {
            //分页算法
            let  rows = (pictures.count - 1) / 3 + 1
            let imageWidth = (screenWidth - 4*12)/3
            let height = imageWidth * CGFloat(rows) + CGFloat((rows - 1 )*12)
            pictureViewSize = CGSize(width: screenWidth - 12 * 2, height: height)
        }
    }
    
    //label文字
    func dealWithRetweeted() {
        if let retweetedStauts = statusModel.retweeted_status,
            let text = retweetedStauts.text,
            let screen_name = retweetedStauts.user?.screen_name {
            retweetedText = "@\(screen_name): \(text)"
        }
    }
    func dealWithStauts () {
        if let timeStr = statusModel.created_at {
            timeString = Date.sinaTimeToDate(sinaTime: timeStr).dateToRequiredString()
        }
        
     if let sourceStr = statusModel.source,
        sourceStr.characters.count > 0 {
        
        let firstRange = sourceStr.range(of: "\">")
        let secondRange = sourceStr.range(of: "</a>")
        
        if let start = firstRange?.upperBound, let end = secondRange?.lowerBound {
            let  range = start..<end
            sourceString = sourceStr.substring(with: range)
          }
        }
    }
    
    func dealWithUser() {
        
        //处理皇冠的等级
        if let mbrank = statusModel.user?.mbrank, mbrank > 0 && mbrank < 7 {
            levelImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        //处理vip图标
        if   let  verified_type = statusModel.user?.verified_type{
            switch verified_type {
            case 0:
                vipImage = UIImage(named: "avatar_vip")
            case 2, 3, 5:
                vipImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                    vipImage = UIImage(named: "avatar_grassroot")
            default:
                vipImage = nil
            }
        }
    }
}










